using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace Microsoft.Modeling.StoryboardDesigner
{
    /// <summary>
    /// Utility class: contains helper routines for updating compartments
    /// </summary>
    internal static class CompartmentHelper
    {
        #region helper method to update compartments
        /// <summary>
        /// Updates the compartments for the shapes associated to the given list of model elements
        /// </summary>
        /// <param name="elements">List of model elements</param>
        /// <param name="shapeType">The type of shape that needs updating</param>
        /// <param name="compartmentName">The name of the compartment to update</param>
        /// <param name="repaintOnly">If true, the method will only invalidate the shape for a repaint, without re-initializing the shape.</param>
        internal static void UpdateCompartments(ModelElement element, global::System.Type shapeType, string compartmentName, bool repaintOnly)
        {
            LinkedElementCollection<PresentationElement> pels = PresentationViewsSubject.GetPresentation(element);
            foreach(PresentationElement pel in pels)
            {
                CompartmentShape compartmentShape = pel as CompartmentShape;
                if(compartmentShape != null && shapeType.IsAssignableFrom(compartmentShape.GetType()))
                {
                    if(repaintOnly)
                    {
                        compartmentShape.Invalidate();
                    }
                    else
                    {
                        foreach(CompartmentMapping mapping in compartmentShape.GetCompartmentMappings())
                        {
                            if(mapping.CompartmentId == compartmentName)
                            {
                                mapping.InitializeCompartmentShape(compartmentShape);
                                break;
                            }
                        }
                    }
                }
            }
        }
        #endregion
    }
}